/*
 * Node.java
 *
 * Created on den 4 maj 2007, 22:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package grex.Graphics;

import grex.GP;
import grex.Options;
import grex.Nodes.ITerminal;
import grex.Nodes.If;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.text.NumberFormat;

import javax.swing.JButton;
public class GUINode extends JButton implements MouseMotionListener,MouseListener,KeyListener{
    private grex.Nodes.Node gene;
    private int iX;
    private GUINode[] nodeChildren;
    private TreePanel treePanel;
    private static NumberFormat nf = NumberFormat.getInstance();
    private double p=0;
    private boolean CLASSIFICATION_TASK = false;
    int n;
    GP gp;
    /** Creates a new instance of Node */
    public GUINode(grex.Nodes.Node gene,TreePanel treePanel,GP gp) {
        this(gene);
        this.gp=gp;
        this.gene=(grex.Nodes.Node) gene.clone();
        this.treePanel = treePanel;
    }
    
    public GUINode(grex.Nodes.Node gene){
        super();
        nf.setMaximumFractionDigits(1);
        if(gene instanceof If){
            grex.Nodes.Node[] children = gene.getChildren();
            this.setText("if " + getGeneAndChildrenAsString(children[0]));
        } else if(gene instanceof ITerminal){
            ITerminal t = (ITerminal)gene;  
            
            if(gene.ops.getPROBLEM_TYPE()==Options.CLASSIFICATION)
                this.setText(gene.toString() + " (P:" + nf.format(t.getProb()*100)+"% N="+t.getCount()+")");
            else
                this.setText(gene.toString() + " N="+t.getCount()+")");
        }else{
            this.setText(gene.toString());
        }
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
        this.addKeyListener(this);
        this.gene=gene;
    }
    public GP getGp(){
        return gp;
    }
    private String getGeneAndChildrenAsString(grex.Nodes.Node gene){
        String condition="",left="",right="",middle="";
        grex.Nodes.Node[] children= gene.getChildren();
        if(children!=null){
            if(children.length==2){
                
            left = getGeneAndChildrenAsString(children[0]);
            right = getGeneAndChildrenAsString(children[1]);
            return "(" +left + " " + gene + " " + right+")"; 
            }else{//IF 3 children
              condition = getGeneAndChildrenAsString(children[0]);  
              left = getGeneAndChildrenAsString(children[1]);
              right = getGeneAndChildrenAsString(children[2]);
              return "if (" + condition +":" + left +":" + right + ")"; 
            }
        }else
            return gene.toString();
        
    }
    public grex.Nodes.Node getGene(){
        return gene;
    }
    
    
    public void setNodeChildren(GUINode[] childs){
        nodeChildren = childs;
    }
    
    public GUINode[] getNodeChildren(){
        return nodeChildren;
    }
    
    public void setIx(int i){
        iX=i;
    }
    
    public int getIx(){
        return iX;
    }
    
    public void mouseDragged(MouseEvent e) {
        //this.setLocation(e.getX()-xDiff,e.getY()-yDiff);
        this.setLocation(e.getXOnScreen()-xDiff,e.getYOnScreen()-yDiff);
        treePanel.repaint();
    }
    
    public void mouseMoved(MouseEvent e) {
    }
    
    public void mouseClicked(MouseEvent e) {
        System.out.println(this.getLocation());
    }
    int xDiff=0;
    int yDiff=0;
    public void mousePressed(MouseEvent e) {
      
        xDiff=e.getXOnScreen()-this.getX();
        yDiff=e.getYOnScreen()-this.getY();
    }
    
    public void mouseReleased(MouseEvent e) {
        
    }
    
    public void mouseEntered(MouseEvent e) {
    }
    
    public void mouseExited(MouseEvent e) {
    }
    
    public void keyTyped(KeyEvent e) {
    }
    
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode()==KeyEvent.VK_DOWN)
            this.setLocation(this.getX(),this.getY()+1);
        else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
            this.setLocation(this.getX()+1,this.getY());
        else if(e.getKeyCode()==KeyEvent.VK_UP)
            this.setLocation(this.getX(),this.getY()-1);
        else if(e.getKeyCode()==KeyEvent.VK_LEFT)
            this.setLocation(this.getX()-1,this.getY());
        treePanel.repaint();
    }
    
    public void keyReleased(KeyEvent e) {
        
    }
    
    
    
    
}
